package com.lostreseditores.sabersai.presentation.backingBeans;

import java.util.List;
import java.util.TimeZone;

import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.event.RowEditEvent;

import com.lostreseditores.sabersai.exceptions.ZMessManager;
import com.lostreseditores.sabersai.model.Jornada;
import com.lostreseditores.sabersai.model.dto.JornadaDTO;
import com.lostreseditores.sabersai.presentation.businessDelegate.BusinessDelegatorView;
import com.lostreseditores.sabersai.utilities.FacesUtils;


public class JornadaView {
    private InputText txtJorndescripv;
    private InputText txtJornidn;
    private CommandButton btnSave;
    private CommandButton btnModify;
    private CommandButton btnDelete;
    private CommandButton btnClear;
    private List<JornadaDTO> data;
    private JornadaDTO selectedJornada;

    public JornadaView() {
        super();
    }

    public void rowEventListener(RowEditEvent e) {
        try {
            JornadaDTO jornadaDTO = (JornadaDTO) e.getObject();

            if (txtJorndescripv == null) {
                txtJorndescripv = new InputText();
            }

            txtJorndescripv.setValue(jornadaDTO.getJorndescripv());

            if (txtJornidn == null) {
                txtJornidn = new InputText();
            }

            txtJornidn.setValue(jornadaDTO.getJornidn());

            action_modify();
        } catch (Exception ex) {
        }
    }

    public String action_clear() {
        if (txtJorndescripv != null) {
            txtJorndescripv.setValue(null);
            txtJorndescripv.setDisabled(true);
        }

        if (txtJornidn != null) {
            txtJornidn.setValue(null);
            txtJornidn.setDisabled(false);
        }

        if (btnSave != null) {
            btnSave.setDisabled(true);
        }

        if (btnDelete != null) {
            btnDelete.setDisabled(true);
        }

        if (btnModify != null) {
            btnModify.setDisabled(true);
        }

        if (btnClear != null) {
            btnClear.setDisabled(false);
        }

        return "";
    }

    public void listener_txtId() {
        Jornada entity = null;

        try {
            Integer jornidn = new Integer(txtJornidn.getValue().toString());
            entity = BusinessDelegatorView.getJornada(jornidn);
        } catch (Exception e) {
            // TODO: handle exception
        }

        if (entity == null) {
            txtJorndescripv.setDisabled(false);
            txtJornidn.setDisabled(false);
            btnSave.setDisabled(false);
            btnDelete.setDisabled(true);
            btnModify.setDisabled(true);
            btnClear.setDisabled(false);
        } else {
            txtJorndescripv.setValue(entity.getJorndescripv());
            txtJorndescripv.setDisabled(false);
            txtJornidn.setValue(entity.getJornidn());
            txtJornidn.setDisabled(true);
            btnSave.setDisabled(true);
            btnDelete.setDisabled(false);
            btnModify.setDisabled(false);
            btnClear.setDisabled(false);
        }
    }

    public String action_save() {
        try {
            BusinessDelegatorView.saveJornada(FacesUtils.checkString(
                    txtJorndescripv), FacesUtils.checkInteger(txtJornidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYSAVED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_delete() {
        try {
            BusinessDelegatorView.deleteJornada(FacesUtils.checkInteger(
                    txtJornidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modify() {
        try {
            BusinessDelegatorView.updateJornada(FacesUtils.checkString(
                    txtJorndescripv), FacesUtils.checkInteger(txtJornidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
            action_clear();
        } catch (Exception e) {
            data = null;
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String actionDeleteDataTableEditable() {
        try {
            if (txtJornidn == null) {
                txtJornidn = new InputText();
            }

            txtJornidn.setValue(selectedJornada.getJornidn());

            BusinessDelegatorView.deleteJornada(FacesUtils.checkInteger(
                    txtJornidn));
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYDELETED);
            action_clear();
            data.remove(selectedJornada);
        } catch (Exception e) {
            FacesUtils.addErrorMessage(e.getMessage());
        }

        return "";
    }

    public String action_modifyWitDTO(String jorndescripv, Integer jornidn)
        throws Exception {
        try {
            BusinessDelegatorView.updateJornada(jorndescripv, jornidn);
            FacesUtils.addInfoMessage(ZMessManager.ENTITY_SUCCESFULLYMODIFIED);
        } catch (Exception e) {
            //renderManager.getOnDemandRenderer("JornadaView").requestRender();
            FacesUtils.addErrorMessage(e.getMessage());
            throw e;
        }

        return "";
    }

    public InputText getTxtJorndescripv() {
        return txtJorndescripv;
    }

    public void setTxtJorndescripv(InputText txtJorndescripv) {
        this.txtJorndescripv = txtJorndescripv;
    }

    public InputText getTxtJornidn() {
        return txtJornidn;
    }

    public void setTxtJornidn(InputText txtJornidn) {
        this.txtJornidn = txtJornidn;
    }

    public List<JornadaDTO> getData() {
        try {
            if (data == null) {
                data = BusinessDelegatorView.getDataJornada();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return data;
    }

    public void setData(List<JornadaDTO> jornadaDTO) {
        this.data = jornadaDTO;
    }

    public JornadaDTO getSelectedJornada() {
        return selectedJornada;
    }

    public void setSelectedJornada(JornadaDTO jornada) {
        this.selectedJornada = jornada;
    }

    public CommandButton getBtnSave() {
        return btnSave;
    }

    public void setBtnSave(CommandButton btnSave) {
        this.btnSave = btnSave;
    }

    public CommandButton getBtnModify() {
        return btnModify;
    }

    public void setBtnModify(CommandButton btnModify) {
        this.btnModify = btnModify;
    }

    public CommandButton getBtnDelete() {
        return btnDelete;
    }

    public void setBtnDelete(CommandButton btnDelete) {
        this.btnDelete = btnDelete;
    }

    public CommandButton getBtnClear() {
        return btnClear;
    }

    public void setBtnClear(CommandButton btnClear) {
        this.btnClear = btnClear;
    }

    public TimeZone getTimeZone() {
        return java.util.TimeZone.getDefault();
    }
}
